<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">原物料编码</span>
                <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">原物料名称</span>
                <el-input v-model="tableParam.name" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">品牌id</span>
                <el-select v-model="tableParam.brandId" placeholder="请选择" style="width: 200px;">
                  <el-option v-for="item in brandlist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">类型</span>
                <el-select v-model="tableParam.typeId" placeholder="请选择" style="width: 200px;">
                  <el-option v-for="item in typelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="addData()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" size="small" class="bottom_three" @click="Update(1)">
                <i class="icon iconfont iconicon_button_startuse" style="font-size: 12px;"></i>
                启用
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="Update(0)">
                <i class="icon iconfont iconicon_button_disable1" style="font-size: 12px;"></i>
                禁用
              </el-button>
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="load()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div v-loading="tableLoading" class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection"></el-table-column>
              <template v-for="(item, index2) in table">
                <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="updateFromDate(scope.row, '修改')">
                      <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                      修改
                    </el-button>
                    <el-button v-if="item.label == '价格'&&ButtonList.findIndex(res=>(res.buttonValue=='JIAGESHEZHI'))!==-1" class="bottom_one" @click="updatepriceFromDate(scope.row)">价格设置</el-button>
                    <span v-if="item.label !== '操作' && item.label !== '状态' && item.label !== '价格'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
          <!-- <div class="card_bottom">
            <div class="card_bottom_left">
              <div class="top_page_totle">总共{{ total }}条</div>
              <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
            </div>
            <div>
              <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
            </div>
          </div> -->
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="companyId">
            <span slot="label" style="color:  #FF0000 ;">公司</span>
            <el-select ref="selection1" v-model="fromDate.companyId" placeholder="请选择" @change="$nextTick(()=>{fromDate.company = $refs.selection1.selectedLabel})">
              <el-option v-for="item in companylist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <span slot="label">ERP物料编码</span>
            <el-input v-model="fromDate.code" placeholder="请填写ERP编码"></el-input>
          </el-form-item>
          <el-form-item prop="rawId">
            <span slot="label" style="color:  #FF0000 ;">原物料名称</span>
            <el-select v-model="fromDate.rawId" placeholder="请选择">
              <el-option v-for="item in rawIdlist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="specificationId">
            <span slot="label" style="color:  #FF0000 ;">规格</span>
            <el-select v-model="fromDate.specificationId" placeholder="请选择">
              <el-option v-for="item in specificationlist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="brandId">
            <span slot="label" style="color:  #FF0000 ;">品牌</span>
            <el-select v-model="fromDate.brandId" placeholder="请选择">
              <el-option v-for="item in brandlist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="typeId">
            <span slot="label" style="color:  #FF0000 ;">类型</span>
            <el-select v-model="fromDate.typeId" placeholder="请选择">
              <el-option v-for="item in typelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="originPlace">
            <span slot="label" style="color: #FF0000 ;">产地</span>
            <el-input v-model="fromDate.originPlace" placeholder="请填写产地"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="fromDate.remark" placeholder="请填写备注" class="textarea" type="textarea" resize="none" rows="5"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button :loading="submitLoading" class="bottom_all" style="margin-left: 24px;" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog v-dialogDrag :visible.sync="priceDialog" width="980px" :close-on-click-modal="false" :before-close="resetprice">
      <span slot="title" class="dialog-title">价格</span>
      <div class="nav-one" style="min-height: 660px;">
        <div class="navone-nav">
          <template v-for="(item, index) in checknavlist">
            <div :key="index" :class="priceDate.type == item.id ? 'active' : ''" @click="priceDate.type  = item.id,priceDate.money=''">{{ item.name }}</div>
          </template>
        </div>
        <div class="navone-info">
          <el-form ref="refpriceDate" :inline="true" :model="priceDate" label-width="110px">
            <el-form-item prop="money">
              <span v-if="priceDate.type == 1" slot="label">信息价格</span>
              <span v-if="priceDate.type == 2" slot="label">内部价格</span>
              <span v-if="priceDate.type == 3" slot="label">市场价格</span>
              <el-input v-model="priceDate.money" placeholder="请输入" oninput="if(value.length > 11 ){ value = value.slice(0,11)}" onkeypress='return( /\d|\./.test(String.fromCharCode(event.keyCode)))' @input="priceDate.money=$regExp.checkNum(priceDate.money)" @blur="priceDate.money=$regExp.blurCheckNum(priceDate.money)">
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item prop="startTime">
              <span slot="label">生效时间</span>
              <el-date-picker v-model="priceDate.startTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button size="mini" class="bottom_all" style="margin-left: 20px;" @click="addpriceDate()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
            </el-form-item>
          </el-form>
          <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="priceTabDate" tooltip-effect="dark" height="464">
            <el-table-column align="center" prop label="序号" width="80">
              <template slot-scope="scope">{{scope.$index+1}}</template>
            </el-table-column>
            <el-table-column align="center" prop="money" label="信息价格（元）"></el-table-column>
            <el-table-column align="center" prop="startTime" label="生效日期"></el-table-column>
            <el-table-column align="center" prop label="操作" width="120">
              <template slot-scope="scope">
                <el-button size="mini" class="bottom_two" @click="deletespriceDate(scope.row)">
                  <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <div style="display: flex;align-items: center;padding-top: 20px;">
            <div class="top_page_totle">总共{{ priceTotal }}条</div>
            <el-pagination :current-page.sync="priceformDate.page" :page-size="priceformDate.pageSize" layout="prev, pager, next, jumper" :total="priceTotal" @current-change="getpriceTabDate()"></el-pagination>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetprice">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="resetprice">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import { getDicType } from "@/api/common.js";
import { permissionsCompanyList } from "@/api/common.js";
import { getUserInfo } from "@/utils/auth.js";
import {
  queryPage,
  batchUpdateOrDelete,
  updateSortNum,
  saveOrUpdate,
  Download,
  queryPageMoney,
  saveOrUpdateMoney,
  deletesMoney,
  qureyCodeIs
} from "@/api/basicConfig/Rmaterial.js";
export default {
  mixins: [mixin],
  data() {
    return {
      UserInfo: '',
      companylist: [],
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      batchUpdateOrDelete: batchUpdateOrDelete,
      updateSortNum: updateSortNum,
      saveOrUpdate: saveOrUpdate,
      DownloadURL: Download,
      /*表格数据*/
      table: [
        {
          label: "原物料编码",
          width: "180",
          prop: "barCode",
          status: true,
        },
        {
          label: "原物料名称",
          width: "120",
          prop: "name",
          status: true,
        },
        {
          label: "品牌",
          width: "130",
          prop: "brand",
          status: true,
        },
        {
          label: "规格",
          width: "130",
          prop: "specification",
          status: true,
        },
        {
          label: "价格",
          width: "100",
          prop: "",
          status: true,
        },
        {
          label: "类型",
          width: "130",
          prop: "typeName",
          status: true,
        },
        {
          label: "产地",
          width: "130",
          prop: "originPlace",
          status: true,
        },
        {
          label: "公司",
          width: "200",
          prop: "company",
          status: true,
        },
        {
          label: "备注",
          width: "220",
          prop: "remark",
          status: true,
        },
        {
          label: "状态",
          width: "100",
          prop: "enable",
          status: true,
        },
        {
          label: "操作",
          width: "120",
          prop: "",
          status: true,
        },
      ],
      /*表单页类型，0为无树状，1为有树状，有树状在el-main加上display:flex*/
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        companyId: [
          {
            required: true,
            message: "请选择所属单位",
            trigger: "change",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入原物料编码",
            trigger: "blur",
          },
        ],
        rawId: [
          {
            required: true,
            message: "请选择原物料名称",
            trigger: "change",
          },
        ],
        typeId: [
          {
            required: true,
            message: "请选择类型",
            trigger: "change",
          },
        ],
        brandId: [
          {
            required: true,
            message: "请选择品牌",
            trigger: "change",
          },
        ],
        specificationId: [
          {
            required: true,
            message: "请选择规格",
            trigger: "change",
          },
        ],
        originPlace: [
          {
            required: true,
            message: "请输入原物料产地",
            trigger: "blur",
          },
        ]
      },
      specificationlist: [],
      brandlist: [],
      rawIdlist: [],
      fromDate: {
        code: "",
        name: "",
        rawId: "",
        typeId: "",
        brandId: "",
        specificationId: "",
        originPlace: '',
        remark: "",
        companyId: '',
        company: '',
      },
      typelist: [],
      priceDialog: false,
      checknavlist: [
        {
          name: "信息价格",
          id: 1,
        },
        {
          name: "内部价格",
          id: 2,
        },
        {
          name: "市场价格",
          id: 3,
        },
      ],
      priceDate: {
        rawMaterialsId: "",
        startTime: "",
        money: "",
        type: 1,
      },
      priceTabDate: [],
      priceTotal: 0,
      priceformDate: {
        page: 1,
        pageSize: 9,
      },
    };
  },
  async created() {
    this.UserInfo = await getUserInfo();
    this.companylist = await permissionsCompanyList();
    /*存储拷贝的原初始化fromDate数据*/
    this.tableParam = { ...this.tableParam, name: "", typeId: "", code: "" };
    this.gettableData();
    getDicType({ code: "MATname" }).then((res) => {
      this.rawIdlist = res.data.items;
    });
    getDicType({ code: "MATtype" }).then((res) => {
      this.typelist = res.data.items;
    });
    getDicType({ code: "MATbrand" }).then((res) => {
      this.brandlist = res.data.items;
    });
    getDicType({ code: "MATspec" }).then((res) => {
      this.specificationlist = res.data.items;
    });
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  watch: {
    //写法1
    "priceDate.type"() {
      this.getpriceTabDate();
    },
  },
  methods: {
    addData() {
      this.addFromDate('添加');
      if (this.companylist.length) {
        this.fromDate.companyId = this.UserInfo.companyId;
        this.fromDate.company = this.UserInfo.companyName;
      }
    },
    codeFun() {
      let json = {
        code: this.fromDate.code,
        id: this.fromDate.id
      }
      qureyCodeIs(json).then((res) => {
        if (res.code == 1001, res.data == false) {
          this.fromDate.code = ''
          this.$message({
            message: '原物料编码已存在',
            type: 'warning'
          });
        }
      })
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          _this.fromDate.name = _this.rawIdlist.find(function (obj) {
            return obj.id == _this.fromDate.rawId;
          }).itemName;
          _this.fromDate.typeName = _this.typelist.find(function (obj) {
            return obj.id == _this.fromDate.typeId;
          }).itemName;
          _this.fromDate.brand = _this.brandlist.find(function (obj) {
            return obj.id == _this.fromDate.brandId;
          }).itemName;
          _this.fromDate.specification = _this.specificationlist.find(function (
            obj
          ) {
            return obj.id == _this.fromDate.specificationId;
          }).itemName;
          _this.submitFromDate()
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    updatepriceFromDate(low) {
      this.priceDate.rawMaterialsId = low.id;
      this.getpriceTabDate();
      this.priceDialog = true;
    },
    getpriceTabDate() {
      queryPageMoney({
        ...this.priceformDate,
        rawMaterialsId: this.priceDate.rawMaterialsId,
        type: this.priceDate.type,
      }).then((res) => {
        this.priceTabDate = res.data.data;
        this.priceTotal = res.data.totalRows;
      });
    },
    addpriceDate() {
      if (this.priceDate.money == '' || this.priceDate.startTime == '') {
        this.$message({
          message: '请选择价格和生效时间后添加',
          type: 'warning'
        })
        return
      }
      saveOrUpdateMoney(this.priceDate).then(() => {
        this.$refs.refpriceDate.resetFields();
        this.getpriceTabDate();
      });
    },
    deletespriceDate(low) {
      this.$confirm("此操作将永久删除该信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "bottom_all_two",
        confirmButtonClass: "bottom_all",
        type: "warning",
      })
        .then(() => {
          let arr = [];
          arr.push(low.id);
          deletesMoney(arr).then(() => {
            this.getpriceTabDate();
          });
        })
        .catch(() => { });
    },
    resetprice() {
      this.priceDialog = false;
    },
    init() {
      /*重置数据*/
      this.tableParam.name = "";
      this.tableParam.code = "";
      this.tableParam.typeId = "";
      this.tableParam.brandId = "";
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
  width: 280px;
}

.table_link {
  margin-left: 10px;
  color: $bg_color;
}
.text {
  padding-left: 10px;
}
.table_link:hover {
  opacity: 0.7;
}
.textarea {
  width: 280px;
}
.navone-info {
  .el-input,
  .el-select {
    width: 250px;
  }
}
</style>
